class PartMemberInfoController {

    def beforeInterceptor = [action:this.&auth]
    
    def index = { redirect(action:list,params:params) }

    def auth = {
        if(!session.adminId) {
            flash['message'] = '尚未登录'
            redirect(uri:'/index.gsp')
            return false
        }
    }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ partMemberInfoInstanceList: PartMemberInfo.list( params ), partMemberInfoInstanceTotal: PartMemberInfo.count() ]
    }

    def show = {
        def partMemberInfoInstance = PartMemberInfo.get( params.id )

        if(!partMemberInfoInstance) {
            flash.message = "党员${params.name}不存在"
            redirect(action:list)
        }
        else { return [ partMemberInfoInstance : partMemberInfoInstance ] }
    }

    def delete = {
        def partMemberInfoInstance = PartMemberInfo.get( params.id )
        if(partMemberInfoInstance) {
            try {
                partMemberInfoInstance.delete(flush:true)
                flash.message = "党员 ${params.name}成功删除"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "PartMemberInfo ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "PartMemberInfo not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def partMemberInfoInstance = PartMemberInfo.get( params.id )

        if(!partMemberInfoInstance) {
            flash.message = "PartMemberInfo not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ partMemberInfoInstance : partMemberInfoInstance ]
        }
    }

    def update = {
        def partMemberInfoInstance = PartMemberInfo.get( params.id )
        if(partMemberInfoInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(partMemberInfoInstance.version > version) {
                    partMemberInfoInstance.errors.rejectValue("version", "partMemberInfo.optimistic.locking.failure", "另一个用户也在更新该党员信息，请稍后再试")
                    render(view:'edit',model:[partMemberInfoInstance:partMemberInfoInstance])
                    return
                }
            }
            partMemberInfoInstance.properties = params
            if(!partMemberInfoInstance.hasErrors() && partMemberInfoInstance.save()) {
                flash.message = "党员${params.name} 的信息已经被修改"
                redirect(action:show,id:partMemberInfoInstance.id)
            }
            else {
                render(view:'edit',model:[partMemberInfoInstance:partMemberInfoInstance])
            }
        }
        else {
            flash.message = "PartMemberInfo not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def partMemberInfoInstance = new PartMemberInfo()
        partMemberInfoInstance.properties = params
        return ['partMemberInfoInstance':partMemberInfoInstance]
    }

    def save = {
        def partMemberInfoInstance = new PartMemberInfo(params)
        if(!partMemberInfoInstance.hasErrors() && partMemberInfoInstance.save()) {
            flash.message = "党员${params.name} 的信息已成功存入系统"
            redirect(action:show,id:partMemberInfoInstance.id)
        }
        else {
            render(view:'create',model:[partMemberInfoInstance:partMemberInfoInstance])
        }
    }

   def searchByDept = {
	if(request.method == 'POST') {
		def result = PartMemberInfo.findAllByDepartmentLike('%' + params.department + '%')
		render(view:'list',model:[ 
		partMemberInfoInstanceList: result,
		partMemberInfoInstanceTotal: result.size() ])
	}
   }
   
   def searchByEduBG = {
	if(request.method == 'POST') {
		def result = PartMemberInfo.findAllByEduBackgroundLike('%' + params.edubg + '%')
		render(view:'list',model:[ 
		partMemberInfoInstanceList: result,
		partMemberInfoInstanceTotal: result.size() ])
	}
   }
   
   def searchByBirthday = {
	if(request.method == 'POST') {
		// fuck datapicker
		def minDate = new Date().parse("yyyyMdd",params["minDate_year"] + params["minDate_month"] + params["minDate_day"])
		def maxDate = new Date().parse("yyyyMdd",params["maxDate_year"] + params["maxDate_month"] + params["maxDate_day"])
		def c = PartMemberInfo.createCriteria()
		def result = c {
			between("birthday", minDate - 1, maxDate)
			maxResults(10)
			order("birthday", "desc")
		}
		render(view:'list',model:[ 
			partMemberInfoInstanceList: result,
			partMemberInfoInstanceTotal: result.size() ])
	}
   }
   
   def searchByJoinDate = {
	if(request.method == 'POST') {
		def minDate = new Date().parse("yyyyMdd",params["minDate_year"] + params["minDate_month"] + params["minDate_day"])
		def maxDate = new Date().parse("yyyyMdd",params["maxDate_year"] + params["maxDate_month"] + params["maxDate_day"])
		def c = PartMemberInfo.createCriteria()
		def result = c {
			between("joinDate", minDate - 1, maxDate)
			maxResults(10)
			order("joinDate", "desc")
		}
		render(view:'list',model:[ 
			partMemberInfoInstanceList: result,
			partMemberInfoInstanceTotal: result.size() ])
	}
   }
   
   def searchByWorkDate = {
	if(request.method == 'POST') {
		def minDate = new Date().parse("yyyyMdd",params["minDate_year"] + params["minDate_month"] + params["minDate_day"])
		def maxDate = new Date().parse("yyyyMdd",params["maxDate_year"] + params["maxDate_month"] + params["maxDate_day"])
		def c = PartMemberInfo.createCriteria()
		def result = c {
			between("workDate", minDate - 1, maxDate)
			maxResults(10)
			order("workDate", "desc")
		}
		render(view:'list',model:[ 
			partMemberInfoInstanceList: result,
			partMemberInfoInstanceTotal: result.size() ])
	}
   }
}
